package org.demo.opengl;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.IntBuffer;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import org.example.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.opengl.GLSurfaceView.Renderer;
import android.opengl.GLUtils;

public class GLRender implements Renderer {
	int				one				= 0x10000;
	float			a, b;

	// 三角形三个顶点
	private int[]	triggerBuffer	= new int[] { 0, one, 0, // 上顶点
			-one, -one, 0, // 左下点
			one, -one, 0,			};			// 右下点
	// 正方形的4个顶点
	private int[]	quaterBuffer	= new int[] { 
			0, 0, 0, 
			one, 0, 0,
			0, one, 0,
			one, one, 0
			};
	private int[]	thrid			= new int[] {//正方体6个面
			//前
			0, 0, -one, 
			one, 0, -one,
			0, -one, -one,
			one, -one, -one,
			//后
			0, 0, 0,
			0, -one, 0,
			one, 0, 0,
			one, -one, 0,
			//坐
			0, 0, 0, 
			0, 0, -one,
			0, -one, 0,  
			0, -one, -one,
			//右
			one, 0, 0,
			one, -one, 0,
			one, 0, -one,
			one, -one, -one,
			//上
			0, 0, 0,
			one, 0, 0,
			0, 0, -one, 
			one, 0, -one, 
			//下
			0, -one, 0,
			one, -one, 0,
			0, -one, -one,
			one, -one, -one, 

									};
	private float[] fourth=new float[]{//纹理顶点
			//前
			0, 0, 
			1f, 0,
			0, -1f,
			1f, -1f,
			//后
			0, 0, 
			0, -1f, 
			1f, 0, 
			1f, -1f, 
			//坐
			 0, 0, 
			0, -1f,
			 -1f, 0,  
			-1f, -1f,
			//右
			 0, 0,
			 -1f, 0,
			 0, -1f,
			 -1f, -1f,
			//上
			0,0,
			1f,  0,
			0,  -1f, 
			1f, -1f, 
			//下
			0,  0,
			1f, 0,
			0,  -1f,
			1f, -1f, 
			};
	
	private Bitmap image;
	public GLRender(Context c) {
		image=BitmapFactory.decodeResource( c.getResources() , R.drawable.robot);
		}

	@Override
	public void onDrawFrame(GL10 gl) {
		// 清除屏幕和深度缓存
		gl.glClear( GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT );
		// 重置当前的模型观察矩阵
		draw_01(gl);
		draw_02(gl);
		draw_03(gl);
		// gl.glFinish();
		// 取消顶点设置
		gl.glDisableClientState( GL10.GL_VERTEX_ARRAY );

	}

	@Override
	public void onSurfaceChanged(GL10 gl, int width, int height) {
		float ratio = (float) width / height;
		// 设置OpenGL场景的大小
		gl.glViewport( 0 , 0 , width , height );
		// 设置投影矩阵
		gl.glMatrixMode( GL10.GL_PROJECTION );
		// 重置投影矩阵
		gl.glLoadIdentity();
		// 设置视口的大小
		gl.glFrustumf( -ratio , ratio , -1 , 1 , 1 , 10 );
		// 选择模型观察矩阵
		gl.glMatrixMode( GL10.GL_MODELVIEW );
		// 重置模型观察矩阵
		gl.glLoadIdentity();

	}

	@Override
	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		// 启用阴影平滑
		gl.glShadeModel( GL10.GL_SMOOTH );

		// 黑色背景
		gl.glClearColor( 0 , 0 , 0 , 0 );

		// 设置深度缓存
		gl.glClearDepthf( one );
		// 启用深度测试
		gl.glEnable( GL10.GL_DEPTH_TEST );
		// 所作深度测试的类型
		gl.glDepthFunc( GL10.GL_LEQUAL );

		// 告诉系统对透视进行修正
		gl.glHint( GL10.GL_PERSPECTIVE_CORRECTION_HINT , GL10.GL_FASTEST );
		gl.glEnable( GL10.GL_LINE_SMOOTH );
		gl.glHint( GL10.GL_LINE_SMOOTH_HINT , GL10.GL_NICEST );
		gl.glEnable( GL10.GL_TEXTURE_2D );
		init(gl);
	}

	private void draw_03(GL10 gl) {
		gl.glLoadIdentity();
		gl.glTranslatef( -1.5f , -1.5f , -6f );
		gl.glRotatef( a , 0 , -1f , 0 );
		ByteBuffer thridByteBuffer = ByteBuffer
				.allocateDirect( thrid.length * 4 );
		thridByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
		IntBuffer thridBuffer = thridByteBuffer.asIntBuffer();
		thridBuffer.put( thrid );
		thridBuffer.position( 0 );
		gl.glVertexPointer( 3 , GL10.GL_FIXED , 0 , thridBuffer );
		// 绘制三角形
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP, 0 , 4 );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 4 , 4 );

		// 这里一共分3次绘制，使用了3种颜色绘制6个面
		// gl.glColor4f( 0.0f , one , 0.0f , one );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 8 , 4 );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 12 , 4 );

		// gl.glColor4f( 0.0f , 0.0f , one , one );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 16 , 4 );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 20 , 4 );
		gl.glFlush();
		gl.glFinish();

	}

	private void draw_02(GL10 gl) {
		// 重置当前的模型观察矩阵
		gl.glLoadIdentity();
		// 左移 1.5 单位，并移入屏幕 6.0
		gl.glTranslatef( 1.5f , 0.0f , -6.0f );
		// gl.gl
		// 设置和绘制正方形
		ByteBuffer colorByteBuffer_1 = ByteBuffer
				.allocateDirect( quaterBuffer.length * 4 );
		colorByteBuffer_1.order( ByteOrder.LITTLE_ENDIAN );
		IntBuffer colorBuffer_1 = colorByteBuffer_1.asIntBuffer();
		colorBuffer_1.put( quaterBuffer );
		colorBuffer_1.position( 0 );
		gl.glVertexPointer( 3 , GL10.GL_FIXED , 0 , colorBuffer_1 );
		gl.glDrawArrays( GL10.GL_TRIANGLE_STRIP , 0 , 4 );
		gl.glFinish();

	}

	private void draw_01(GL10 gl) {
		gl.glLoadIdentity();

		// 左移 1.5 单位，并移入屏幕 6.0
		gl.glTranslatef( -1.5f , 0.0f , -6.0f );
		gl.glRotatef( a , 0.5f , 1f , 0.5f );
		// 允许设置顶点
		gl.glEnableClientState( GL10.GL_VERTEX_ARRAY );
		// 设置三角形
		gl.glEnable( GL10.GL_LINE_SMOOTH_HINT );
		ByteBuffer colorByteBuffer = ByteBuffer
				.allocateDirect( triggerBuffer.length * 4 );
		colorByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
		IntBuffer colorBuffer = colorByteBuffer.asIntBuffer();
		colorBuffer.put( triggerBuffer );
		colorBuffer.position( 0 );
		gl.glVertexPointer( 3 , GL10.GL_FIXED , 0 , colorBuffer );
		// 绘制三角形
		gl.glDrawArrays( GL10.GL_TRIANGLES , 0 , 3 );
		a = a + 0.5f;
	}
	private void draw_04(GL10 gl){
		int[] text_temp=new int[1];
		gl.glGenTextures( 1 , text_temp,0 );
		int texture=text_temp[0];
		gl.glBindTexture( GL10.GL_TEXTURE_2D , texture );
		GLUtils.texImage2D(GL10.GL_TEXTURE_2D , 0 , image ,0 );
				
	}
	private void init(GL10 gl){
		ByteBuffer textByteBuffer = ByteBuffer
				.allocateDirect( fourth.length * 4 );
		textByteBuffer.order( ByteOrder.LITTLE_ENDIAN );
		FloatBuffer texBuffer = textByteBuffer.asFloatBuffer();
		texBuffer.put( fourth );
		texBuffer.position( 0 );
        gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texBuffer);
        gl.glEnableClientState(GL10.GL_TEXTURE_COORD_ARRAY);
        draw_04(gl);
	}
}
